
#include "pointBase.h"

CPointBase::CPointBase(CPointSink* pSink, DWORD inType)
: m_pPointSink(pSink)
, m_bCanSend(TRUE)
, m_dwStatus(0)
, m_dwUsrDownCount(0)
, m_nRoomID(0)
{
	m_dwLiveTick = GetTickCountEx();
	RT_STATE_TRACE_THIS("CPointBase::CPointBase lasttick: "<<m_dwLiveTick);
}

CPointBase::~CPointBase()
{
	while (!m_CacheMsgList.empty())
	{
		CRtMessageBlock* pMb = m_CacheMsgList.front();
		pMb->DestroyChained();
		m_CacheMsgList.pop_front();
	}
	m_CacheMsgList.clear();
	RT_INFO_TRACE_THIS("CPointBase::~CPointBase");
}

void CPointBase::OnTimer(CRtTimerWrapper* aID)
{
	
}

void CPointBase::SetUserInfo(WORD rmid, const s_UserInfo& info)
{
	m_userInfoMap[rmid] = info;
	if (0 == rmid){
		m_defaultUser = info;
	}
}

s_UserInfo& CPointBase::GetUserInfo(WORD rmid)
{
	UserInfoMapType::iterator it = m_userInfoMap.find(rmid);
	if (it != m_userInfoMap.end()){
		return it->second;
	}
	else{
		return m_defaultUser;
	}
}

RtResult CPointBase::SendMsg(CRtMessageBlock& msg)
{
	RtResult rv = RT_ERROR_FAILURE;
	if (m_CacheMsgList.empty())
	{
		rv = SendData(msg);
	}
	
	if (RT_FAILED(rv) || !m_bCanSend)
	{
		m_CacheMsgList.push_back(msg.DuplicateChained());
 		RT_INFO_TRACE("CPointBase::SendMsg push_back in list.");
		SendCacheData();
	}
	return rv;
}

void CPointBase::SendCacheData()
{
	while (!m_CacheMsgList.empty())
	{
		CRtMessageBlock* pMb = m_CacheMsgList.front();
		if (RT_FAILED(SendData(*pMb))){
	 		RT_INFO_TRACE("CPointBase::SendCacheData push_back in list.");	
			m_bCanSend = FALSE;
			return;
		}
		pMb->DestroyChained();
		m_CacheMsgList.pop_front();
	}
}

WORD CPointBase::GetRoomID()
{
	return m_nRoomID;
}



